package day26;

import java.lang.reflect.Field;

public class Homework3 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Set s=new Set();
        s.setName("张三");
        System.out.println("修改之前:"+s.getName());
        setProperty(s,"name","李四");
        System.out.println("修改之后:"+s.getName());

        System.out.println("******获取属性*****");
        s.setSex("男");
        String sex=getProperty(s,"sex");
        System.out.println("获取的性别:"+sex);
    }

    public static void setProperty(Object o,String name,Object params) throws NoSuchFieldException, IllegalAccessException {
        Class c=o.getClass();
        Field f=c.getDeclaredField(name);
        f.setAccessible(true);
        f.set(o,params);
    }

    public static String getProperty(Object o, String sex) throws NoSuchFieldException, IllegalAccessException {
        Class c=o.getClass();
        Field f=c.getDeclaredField(sex);
        f.setAccessible(true);
        f.get(o);
        return sex;
    }
}

class Set{
    String name;
    String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}